package com.example.teamtribe.main.seek;

import android.os.Bundle;
import android.util.Log;

import com.example.teamtribe.common.Back;
import com.example.teamtribe.json.JsonIds;
import com.example.teamtribe.utils.HttpUtil;
import com.example.teamtribe.utils.URLStore;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class SeekModel {

    public void getUserByName(String username, final Back back) {
        Map<String, String> params = new HashMap<>(1);
        params.put("username", username);
        HttpUtil.post(URLStore.SEARCH_NAME, params, userIdsCallback(back));
    }

    public void getUserByPhone(String phone, final Back back) {
        Map<String, String> params = new HashMap<>(1);
        params.put("phone", phone);
        HttpUtil.post(URLStore.SEARCH_PHONE, params, userIdCallback(back));
    }

    public void getUserByEmail(String email, Back back) {
        Map<String, String> params = new HashMap<>(1);
        params.put("email", email);
        HttpUtil.post(URLStore.SEARCH_EMAIL, params, userIdCallback(back));
    }

    private Callback userIdCallback(final Back back) {
        return new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                back.failure(HttpUtil.getFailureMeg());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                Log.d("搜索", str);
                try {
                    JSONObject object = new JSONObject(str);
                    int status = object.getInt("code");
                    if (status == 0) {
                        Bundle bundle = new Bundle();
                        ArrayList<Integer> id = new ArrayList<>(1);
                        id.add(object.getInt("id"));
                        bundle.putIntegerArrayList("id", id);
                        back.success(bundle);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        };
    }

    private Callback userIdsCallback(final Back back) {
        return new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                back.failure(HttpUtil.getFailureMeg());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                JsonIds json = new Gson().fromJson(response.body().string(), JsonIds.class);
                int status = json.getCode();
                if (status == 0) {
                    Bundle bundle = new Bundle();
                    bundle.putIntegerArrayList("id", json.getId());
                    back.success(bundle);
                }
            }
        };
    }

}
